package cn.kdyzm.component.xxljob.config;

import cn.kdyzm.component.xxljob.executor.impl.XxlJobSpringExecutor;
import cn.kdyzm.component.xxljob.property.XxlJobProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.commons.util.InetUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * @author kdyzm
 * @date 2021/5/7
 */
@Configuration
@ComponentScan(basePackages = {"cn.kdyzm.component.xxljob"})
@EnableConfigurationProperties
public class Config {

    @Bean
    @ConditionalOnMissingBean
    public XxlJobSpringExecutor xxlJobExecutor(InetUtils inetUtils, XxlJobProperty xxlJobProperty) {
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(xxlJobProperty.getAdmin().getAddresses());
        xxlJobSpringExecutor.setAppname(xxlJobProperty.getExecutor().getAppname());
        xxlJobSpringExecutor.setAccessToken(xxlJobProperty.getAccessToken());
        xxlJobSpringExecutor.setLogPath(xxlJobProperty.getExecutor().getLogpath());
        xxlJobSpringExecutor.setLogRetentionDays(xxlJobProperty.getExecutor().getLogretentiondays());

        String ipAddress = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress();
        xxlJobSpringExecutor.setIp(ipAddress);
        xxlJobSpringExecutor.setPort(xxlJobProperty.getExecutor().getPort());

        return xxlJobSpringExecutor;
    }
}
